package com.dylovedz.javafxphone.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class PhoneUtil {

    /**
     * 提供与电话号码相关的运营商信息
     */
    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

    /**
     * 提供与电话号码有关的地理信息
     */
    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * 中国大陆区区号
     */
    private final static int COUNTRY_CODE = 86;

    /**
     * 搜狗接口地址
     */
    private final static String sogouUrl = "https://www.sogou.com/websearch/phoneAddress.jsp";

    /**
     * 功能：根据手机号 判断手机运营商
     *
     * @param phoneNumber 手机号码
     * @return 如：广东省广州市移动
     */
    public static String getCarrier(String phoneNumber) {

        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        // 返回结果只有英文，自己转成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        switch (carrierEn) {
            case "China Mobile":
                carrierZh += "中国移动";
                break;
            case "China Unicom":
                carrierZh += "中国联通";
                break;
            case "China Telecom":
                carrierZh += "中国电信";
                break;
            default:
                break;
        }
        return carrierZh;
    }

    /**
     * 功能：根据手机号 获取手机归属地
     * @param phoneNumber 手机号码
     * @return 如：广东省广州市
     */
    public static String getGeo(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
    }

    public static boolean isValid(String phoneNumber) throws NumberParseException {
        if (StrUtil.isBlank(phoneNumber)) {
            return false;
        }
        if (!cn.hutool.core.util.PhoneUtil.isMobile(phoneNumber)) {
            return false;
        }
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        Phonenumber.PhoneNumber phone = phoneUtil.parse(phoneNumber, "CN");
        return phoneUtil.isValidNumber(phone);
    }

    /**
     * 根据接口获取手机号归属地信息
     * @return
     */
    public static Map<String, String> getPhoneByHttp(String phoneNumber) {
        Map<String, String> phoneInfo = new HashMap<>();
        String url = sogouUrl + "?phoneNumber=" + phoneNumber.substring(0, 7);
        List<String> res = StrUtil.split(HttpUtil.get(url), "\"");
        if (res.size() > 1) {
            List<String> geoRes = StrUtil.split(res.get(1), " ");
            phoneInfo.put("geo", geoRes.get(0));
            phoneInfo.put("carrier", geoRes.get(1));
        }
        return  phoneInfo;
    }
}
